package com.hand.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author LanceLee
 */
@Component
public class DemoUserDetailsServiceImpl implements UserDetailsService {

//    此处可以通过注解加入数据库对象
//    @Autowired
//    private Dao dao;

    private Logger logger = LoggerFactory.getLogger(getClass());

    //此处用来将数字加密，模拟数据库加密后的密码
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("登陆用户名:" + username);

        String password = passwordEncoder.encode("123456");
        logger.info("数据库密码是:" + password);

        //根据用户名利用dao查找用户信息，并返回
        //根据查找到的用户判断用户是否被冻结
        return new User(username, password,
                true, true, true, true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }

}